package com.qire.manhua.model.viewModel.impl;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.qire.antsbinder.viewModel.ViewModelFactory;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.RoomCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.StartupConfigInfo;
import com.qire.common.constant.StartupConfigInfo.UserWelfareTask;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.widget.adapter.pager.SimpleFragmentPagerAdapter;
import com.qire.common.widget.adapter.pager.SimpleFragmentPagerAdapter.FragmentWrapper;
import com.qire.common.widget.animation.AnimationBinder;
import com.qire.manhua.R;
import com.qire.manhua.databinding.FragmentRecommendBinding;
import com.qire.manhua.fragment.RecommendFragment;
import com.qire.common.model.enumeration.RewardType;
import com.qire.manhua.view.animator.SwingAnimationBinder;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;

/**
 * 首页-推荐页
 */
public class RecommendViewModel extends BaseViewModel {

    private final SwingAnimationBinder swingAnimation = new SwingAnimationBinder();

    private RecommendVipZoneViewModel vipZoneViewModel;
    private RecommendHotViewModel hotViewModel;
    private RecommendUpdateViewModel updateViewModel;
    private RecommendGirlsViewModel girlsViewModel;
    private RecommendBoysViewModel boysViewModel;
    private RecommendNewComicViewModel newComicViewModel;

    public RecommendViewModel() {
        setTaskIconRid(R.drawable.app_home_recommend_icon_task_a);
        setMessageIconRid(R.drawable.app_home_recommend_icon_message_a);
    }

    //region    #附加管理 begin

    /**
     * 附加属性绑定
     */
    public void onAddition(RecommendFragment fragment) {

        vipZoneViewModel = ViewModelFactory.crate(fragment.getActivity(), RecommendVipZoneViewModel.class);
        hotViewModel = ViewModelFactory.crate(fragment.getActivity(), RecommendHotViewModel.class);
        updateViewModel = ViewModelFactory.crate(fragment.getActivity(), RecommendUpdateViewModel.class);
        girlsViewModel = ViewModelFactory.crate(fragment.getActivity(), RecommendGirlsViewModel.class);
        boysViewModel = ViewModelFactory.crate(fragment.getActivity(), RecommendBoysViewModel.class);
        newComicViewModel = ViewModelFactory.crate(fragment.getActivity(), RecommendNewComicViewModel.class);

        Fragment vipZoneFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.RecommendVipZoneFragment)
                .withData("viewModel", vipZoneViewModel)
                .get();
        Fragment hotFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.RecommendHotFragment)
                .withData("viewModel", hotViewModel)
                .get();
        Fragment updateFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.RecommendUpdateFragment)
                .withData("viewModel", updateViewModel)
                .get();
        Fragment girlsFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.RecommendGirlsFragment)
                .withData("viewModel", girlsViewModel)
                .get();
        Fragment boysFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.RecommendBoysFragment)
                .withData("viewModel", boysViewModel)
                .get();
        Fragment newComicFragment = AntsRouter.ROUTER.<RoomCard>buildCard(Navigation.Fragment.RecommendNewComicFragment)
                .withData("viewModel", newComicViewModel)
                .get();

        SimpleFragmentPagerAdapter recommendPagerAdapter = new SimpleFragmentPagerAdapter(fragment.getChildFragmentManager(), new FragmentWrapper[]{
                new FragmentWrapper("VIP专区", vipZoneFragment),
                new FragmentWrapper("推荐", hotFragment),
                new FragmentWrapper("更新", updateFragment),
                new FragmentWrapper("女生", girlsFragment),
                new FragmentWrapper("男生", boysFragment),
                new FragmentWrapper("新作", newComicFragment),
        }, false);

        FragmentRecommendBinding viewDataBinder = fragment.viewDataBinder();
        viewDataBinder.recommendViewPager.setAdapter(recommendPagerAdapter);
        viewDataBinder.recommendTabLayout.setViewPager(viewDataBinder.recommendViewPager);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
    }
    //endregion #附加管理 end

    // region #UI操作
    // 更新顶部搜索栏UI
    public void setTopBarAlpha(int alpha) {
        set("topBarAlpha", alpha);
    }
    public void setSearchButtonTextColor(String textColor) {
        set("searchButtonTextColor", textColor);
    }
    public void setSearchButtonIcon(int buttonIconRid) {
        set("searchButtonIcon", buttonIconRid);
    }
    public void setSearchButtonBgAlpha(int alpha) {
        set("searchButtonBgAlpha", alpha);
    }
    public void setTaskIconRid(int iconRid) {
        set("taskIconRid", iconRid);
    }
    public void setMessageIconRid(int iconRid) {
        set("messageIconRid", iconRid);
    }

    // 更新与用户信息相关的UI，如消息、任务、登录奖励浮层等等
    public void refreshUserRelatedUI() {
        int taskCount = UserEntity.self.getTaskCount() + UserEntity.self.getTaskRewardCount();
        int messageCount = UserEntity.self.getMessageCount();
        set("taskBuoyVisible", taskCount > 0 ? View.VISIBLE : View.GONE);
        set("taskBuoyText", "");
        set("messageBuoyVisible", messageCount > 0 ? View.VISIBLE : View.GONE);
        set("messageBuoyText", "");

        // NOTE: 如果用户登录了则关闭任务浮层，如果用户未登录则显示登录奖励浮层，目前绑定任务以取消不在考虑
        if(UserEntity.self.isLogin()) {
            closeFloatGuideLoginLayer();
        } else {
            showFloatGuideLoginLayer();
        }

        vipZoneViewModel.refreshVipPanelUI();
    }

    // 更新底部用户福利任务引导UI
    public void showFloatGuideLoginLayer() {
        set("floatGuideLoginLayerVisible", View.VISIBLE);
        refreshFloatGuideLoginLayer();
        swingAnimation.goPlay();
    }
    public void closeFloatGuideLoginLayer() {
        set("floatGuideLoginLayerVisible", View.GONE);
        swingAnimation.pause();
    }
    public void refreshFloatGuideLoginLayer() {
        set("floatGuideLoginTextIcon", R.drawable.app_home_recommend_float_icon_phone);
        set("floatGuideLoginText", buildFloatGuideLoginText());
        set("floatGuideLoginBtnText", UserEntity.self.isLogin() ? "点击绑定" : "点击登录");
    }
    private CharSequence buildFloatGuideLoginText() {
        UserWelfareTask loginReward = StartupConfigInfo.INFO.userLoginWelfareTask();
        UserWelfareTask bindReward = StartupConfigInfo.INFO.userBindWelfareTask();
        if(ObjectUtil.isNull(loginReward) || ObjectUtil.isNull(bindReward)) {
            return "";
        }

        SpannableStringBuilder builder = new SpannableStringBuilder();
        int start, end;
        if(UserEntity.self.isLogin()) {
            builder.append("绑定手机即可领取");
            start = builder.length();
            builder.append(String.valueOf(bindReward.rewardValue));
        } else {
            builder.append("新用户登录即可领取");
            start = builder.length();
            builder.append(String.valueOf(loginReward.rewardValue));
        }

        RewardType rewardType = UserEntity.self.isLogin() ? bindReward.rewardType : loginReward.rewardType;

        if(rewardType == RewardType.Bean) {
            end = builder.length();
            builder.append("金豆");
        } else {
            builder.append("天");
            end = builder.length();
            builder.append("会员");
        }

        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.rgb(0xff, 0x51, 0x04));
        builder.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return builder;
    }
    // endregion

    // region #UI属性绑定
    /** 顶部搜索栏关联UI */
    public MutableLiveData<Integer> getTopBarAlpha() {
        return get("topBarAlpha");
    }
    public MutableLiveData<String> getSearchButtonTextColor() {
        return get("searchButtonTextColor");
    }
    public MutableLiveData<Integer> getSearchButtonIcon() {
        return get("searchButtonIcon");
    }
    public MutableLiveData<Integer> getSearchButtonBgAlpha() {
        return get("searchButtonBgAlpha");
    }
    public MutableLiveData<Integer> getTaskIconRid() {
        return get("taskIconRid");
    }
    public MutableLiveData<Integer> getMessageIconRid() {
        return get("messageIconRid");
    }

    /** 顶部任务提示红点 */
    public MutableLiveData<Integer> getTaskBuoyVisible() {
        return get("taskBuoyVisible");
    }
    public MutableLiveData<String> getTaskBuoyText() {
        return get("taskBuoyText");
    }

    /** 顶部消息提示红点 */
    public MutableLiveData<Integer> getMessageBuoyVisible() {
        return get("messageBuoyVisible");
    }
    public MutableLiveData<String> getMessageBuoyText() {
        return get("messageBuoyText");
    }

    /** 底部用户福利任务引导浮层 */
    public AnimationBinder getSwingAnimation() {
        return swingAnimation;
    }
    public MutableLiveData<Integer> getFloatGuideLoginLayerVisible() {
        return get("floatGuideLoginLayerVisible");
    }
    public MutableLiveData<Integer> getFloatGuideLoginTextIcon() {
        return get("floatGuideLoginTextIcon");
    }
    public MutableLiveData<CharSequence> getFloatGuideLoginText() {
        return get("floatGuideLoginText");
    }
    public MutableLiveData<CharSequence> getFloatGuideLoginBtnText() {
        return get("floatGuideLoginBtnText");
    }
    // endregion

}
